<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Common\Model\CaozuologModel;
class ProjectController extends AdminController{
	public function _initialize(){
		parent::_initialize();
	}
	/**
	 * 获取矿机
	 */
	public function getAllProject(){
	    $order = '';
	    $id = I('id');
	    $name = I('name');
	    $begin = I('begin');
	    $last = I('last');
	    $field = 'id,name,suanli,money,status,shouyi,pic';
	    if(!empty($id)){
	        $where['id'] = $id;
	        $field = 'id,name,suanli,money,status,shouyi,pic';
	    }
	    if(!empty($name)){
	        $where['name'] = ['like','%'.$name.'%'];
	    }
	    if(empty($begin)){
	        $begin = 0;
	    }else{
	        $begin = $begin/1000;
	    }
	    if(empty($last)){
	        $last = time();
	    }else{
	        $last = $last/1000+24*3600;
	    }
	    $where['add_time'] = ['between',[$begin,$last]];
	    $page = I('page');
	    $num = I('num');
	    if(empty($page)){
	        $page = 1;
	    }
	    if(empty($num)){
	        $num = 10;
	    }
	    $r = M('Project')
		    ->field($field)
		    ->where($where)
		    ->limit(($page-1)*$num,$num)
		    ->order($order)
	    ->select();
	    $data = [
	        'data'=>$r,
	        'status'=>1,
	    ];
	    $this->ajaxReturn($data);
	}
	/**
	 * 新增矿机
	 */
	public function addProject(){
		$name = I('name');
		if(empty($name)){
			$data = [
			'status'=>-1,
			'msg'=>'缺少矿机名'
					];
			$this->ajaxReturn($data);
		}
		$suanli = I('suanli');
		if(empty($suanli)){
			$data = [
			'status'=>-1,
			'msg'=>'请输入算力'
					];
			$this->ajaxReturn($data);
		}
		$money = I('money');
		if(empty($money)){
			$data = [
			'status'=>-1,
			'msg'=>'请输入金额'
					];
			$this->ajaxReturn($data);
		}
		$shouyi = I('shouyi');
		if(empty($shouyi)){
			$data = [
			'status'=>-1,
			'msg'=>'请输入收益'
					];
			$this->ajaxReturn($data);
		}
		$pic =I('pic');
		if(empty($pic)){
		    $data = [
		        'status'=>-1,
		        'msg'=>'请上传图片'
		    ];
		    $this->ajaxReturn($data);
		}
		$status = I('status');
		if(!empty($status)){
		    if(!in_array($status, [1,-1])){
		        $data = [
		            'status'=>-1,
		            'msg'=>'参数错误'
		        ];
		        $this->ajaxReturn($data);
		    }
		}
		$insert = [
			'name'=>$name,
			'suanli'=>$suanli,
			'money'=>$money,
			'shouyi'=>$shouyi,
			'status'=>$status,
			'pic'=>$pic,
		];
		$r = M('Project')->add($insert);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'新增成功'
				];
		$CAO_LOG = new CaozuologModel();
		$CAO_LOG->addLog($this->user['username'], '新增矿机，矿机名为'.$name);
		$this->ajaxReturn($data);
	}
	/**
	 * 修改矿机
	 */
	public function changeProject(){
		$id = I('id');
		if(empty($id)||!M('Project')->where(['id'=>$id])->find()){
			$return = [
			'status'=>-1,
			'msg'=>'未查询到此数据'
					];
			$this->ajaxReturn($return);
		}
		$pic =I('pic');
		$data = [];
		$name = I('name');
		if(!empty($name)){
			$data['name'] = $name;
		}
		$suanli = I('suanli');
		if(!empty($suanli)){
			$data['suanli'] = $suanli;
		}
		$money = I('money');
		if(!empty($money)){
			$data['money'] = $money;
		}
		$shouyi = I('shouyi');
		if(!empty($shouyi)){
			$data['shouyi'] = $shouyi;
		}
		if(!empty($pic)){
			$data['pic'] = $pic;
		}
		$status = I('status');
		if(!empty($status)){
		    if(!in_array($status, [1,-1])){
		        $data = [
		            'status'=>-1,
		            'msg'=>'参数错误'
		        ];
		        $this->ajaxReturn($data);
		    }
		}
		$data['status'] = $status;
		if(count($data)==0){
			$return = [
			'status'=>-1,
			'msg'=>'您没有修改任何信息'
					];
			$this->ajaxReturn($return);
		}
		$r = M('Project')->where(['id'=>$id])->save($data);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'修改成功'
				];
		$CAO_LOG = new CaozuologModel();
		$CAO_LOG->addLog($this->user['username'], '修改矿机，id为'.$id);
		$this->ajaxReturn($data);
	}
	/**
	 * 删除矿机
	 */
	public function deleteProject(){
		$id = I('id');
		if(empty($id)||!M('Project')->where(['id'=>$id])->find()){
			$return = [
			'status'=>-1,
			'msg'=>'未查询到此数据'
					];
			$this->ajaxReturn($return);
		}
		$r = M('Project')->delete($id);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'删除成功'
				];
		$CAO_LOG = new CaozuologModel();
		$CAO_LOG->addLog($this->user['username'], '删除矿机,id为'.$id);
		$this->ajaxReturn($data);
	}
	
}